<?php
namespace Home\Controller;
use Think\Controller;

// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class BaseController extends Controller{
    //后台基础类构造
    protected $lang_pack;
    public function __construct()
    {
        parent::__construct();
        $GLOBALS['request'] = $_REQUEST;
        $GLOBALS['db']= M();

        if(file_exists(APP_ROOT_PATH.'/system/template/template.php')){
            require_once APP_ROOT_PATH.'/system/template/template.php';
            $GLOBALS['tmpl'] = new \AppTemplate ;
        }
        //require_once APP_ROOT_PATH.'/system/common.php';
        //重新处理后台的语言加载机制，后台语言环境配置于后台config.php文件
        $langSet = conf('DEFAULT_LANG');
        // 定义当前语言
        define('LANG_SET',strtolower($langSet));
        // 读取项目公共语言包
        if (is_file(APP_ROOT_PATH.'/Application/Home/Lang/'.$langSet.'/'.$langSet.'.php'))
        {
            $GLOBALS['lang'] = include APP_ROOT_PATH.'/Application/Home/Lang/'.$langSet.'/'.$langSet.'.php';
        }
        $user_info = \es_session::get("user_info");
        if($user_info && empty($GLOBALS['user_info'])){
            $GLOBALS['user_info'] = $user_info;
        }

        $from_refer = '';
        $from_refer = isset($_GET['r']) ? $_GET['r']: 0;
        $from_refer = intval($from_refer);
        $from = \es_session::get('from_who');
        if(empty($from)){
            \es_session::set('from_who',$from_refer);
            $from = $from_refer ;
        }

        if( $from_refer > 0  || $from){

            if(empty($from)){
                //第一次
                //判断是否已经建立关系

                if($user_info['id']){

                    $rs_bind = M('user')->where('id='.$user_info['id'])->find();

                    if(empty($rs_bind['pid'])){
                        //\es_session::set('from',$from_refer);

                        if($user_info['id'] != $from){
                            $updateData['pid'] = $from ;
                            M('user')->where('id='.$user_info['id'])->save($updateData);
                        }
                    }
                }
            }else{
                //第二次链接打开
                \es_session::set('from_who',$from_refer);
                if($user_info['id']){
                    $rs_bind = M('user')->where('id='.$user_info['id'])->find();

                    if(empty($rs_bind['pid'])){
                        //\es_session::set('from',$from_refer);
                        if($rs_bind['pid'] != $from){
                            $updateData['pid'] = $from ;
                            M('user')->where('id='.$user_info['id'])->save($updateData);

                        }
                    }
                }
            }
        }
        \es_session::set('from_who',1);

    }

    public function check_is_login(){
        $is_login =  0 ;
        $user_info = \es_session::get("user_info");
        if($user_info){
            $is_login =  1 ;
        }
        return $is_login ;
    }

    protected function error($message,$ajax = 0)
    {

        if(!$this->get("jumpUrl"))
        {
            if($_SERVER["HTTP_REFERER"]) $default_jump = $_SERVER["HTTP_REFERER"]; else $default_jump = u("Index/main");
            $this->assign("jumpUrl",$default_jump);
        }
        parent::error($message,$ajax);
    }
    protected function success($message,$ajax = 0)
    {

        if(!$this->get("jumpUrl"))
        {
            if($_SERVER["HTTP_REFERER"]) $default_jump = $_SERVER["HTTP_REFERER"]; else $default_jump = u("Index/main");
            $this->assign("jumpUrl",$default_jump);
        }
        parent::success($message,$ajax);
    }
    function getControllerName(){
        return CONTROLLER_NAME;
    }
}
?>